package com.shuai.ai_core.controller;

import com.shuai.ai_core.dto.ChatReq;
import com.shuai.ai_core.dto.ChatRps;
import com.shuai.ai_core.service.AIInteractService;
import com.shuai.common.constant.AuthenticationConstant;
import com.shuai.common.rps.BaseResponse;
import com.shuai.common.utils.MyJWTUtil;
import com.shuai.common.utils.ResultUtils;
import org.springframework.ai.chat.ChatResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.time.LocalDate;

@RestController
@RequestMapping("/interact")
public class AIInteractController {
    @Autowired
    private AIInteractService aiInteractService;

    @PostMapping("/commonPrint")
    public BaseResponse<ChatRps> interactCommonPrint(@RequestBody ChatReq chatReq,
                                                     @RequestHeader(AuthenticationConstant.AUTHENTICATION_HEADER) String authentication) {
        ChatResponse chatResponse = aiInteractService.interactCommonPrint(chatReq,
                MyJWTUtil.getPayLoadItem(authentication, AuthenticationConstant.PAYLOAD_USERID).toString());
        ChatRps chatRps = new ChatRps();
        chatRps.setContent(chatResponse.getResult().getOutput().getContent());
        chatRps.setLocalDate(LocalDate.now());
        return ResultUtils.success(chatRps);
    }

    @GetMapping(value = "/streamPrint", produces = "text/event-stream")
    public Flux<String> interactStreamPrint(String question,
                                            Long sessionId,
                                            @RequestHeader(AuthenticationConstant.AUTHENTICATION_HEADER) String authentication) {
        ChatReq chatReq = new ChatReq();
        chatReq.setQuestion(question);
        chatReq.setSessionId(sessionId);
        return aiInteractService.interactStreamPrint(chatReq,
                (String) MyJWTUtil.getPayLoadItem(authentication, AuthenticationConstant.PAYLOAD_USERID));
    }
}
